package com.kcl.dfss;


import java.io.ByteArrayOutputStream;
import java.io.File;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;

public class SelectPicPopupd extends Activity implements OnClickListener{

	private static final int NONE = 0;
	private static final int PHOTO_GRAPH = 1;	//拍照
	private static final int PHOTO_ZOOM = 2;	//缩放
	private static final int PHOTO_RESOULT = 3;	//结果
	private static final String IMAGE_UNSPECIFIED = "image/*";
	private static final String tag = "SelectPicPopupd";
	private String path;
	
	private Button btn_take_photo,btn_pick_photo,btn_cancel;
	private LinearLayout layout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.alert_dialog);
		
		btn_take_photo = (Button) this.findViewById(R.id.btn_take_photo);	//拍照
		btn_pick_photo = (Button) this.findViewById(R.id.btn_pick_photo);	//从相册获取照片
		btn_cancel = (Button) this.findViewById(R.id.btn_cancel);
		
		layout = (LinearLayout) findViewById(R.id.pop_layout);
		layout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
			}
		});
		
		btn_take_photo.setOnClickListener(this);
		btn_pick_photo.setOnClickListener(this);
		btn_cancel.setOnClickListener(this);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		finish();
		return true;
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_take_photo:
			Intent intnt = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			intnt.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"temp.jpg")));
			startActivityForResult(intnt, PHOTO_GRAPH);
			break;
		case R.id.btn_pick_photo:
//			Intent intent = new Intent(Intent.ACTION_PICK,null);
//			intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);
//			startActivityForResult(intent, PHOTO_ZOOM);
			Intent intent = new Intent();
			intent.setType(IMAGE_UNSPECIFIED);
			intent.setAction(Intent.ACTION_GET_CONTENT);
			startActivityForResult(intent, PHOTO_ZOOM);
			break;
		case R.id.btn_cancel:
			
			break;
		default:
			break;
		}
		finish();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode == NONE){
			return;
		}
		//拍照
		if(requestCode == PHOTO_GRAPH){
			Log.i(tag, "相机回调执行到");
			//设置文件保存路径
			File picture = new File(Environment.getExternalStorageDirectory()+"/temp.jpg");
			path = Environment.getExternalStorageDirectory()+"/temp.jpg";
			startPhotoZoom(Uri.fromFile(picture));
		}
		if(data == null){
			return;
		}
		//读取相册缩放照片
		if(requestCode == PHOTO_ZOOM){
			Log.i(tag, "相册回调执行到");
			Uri uri = data.getData();
			startPhotoZoom(uri);
		}
		//处理结果
		if(requestCode == PHOTO_RESOULT){
			try{
				Bitmap bm = null;
				Uri originalUri = data.getData();
				ContentResolver resolver = getContentResolver();
				try {
					bm = MediaStore.Images.Media.getBitmap(resolver, originalUri);
				} catch (Exception e) {
					e.printStackTrace();
				}
				String[] proj = {MediaStore.Images.Media.DATA};
				Cursor cursor = managedQuery(originalUri, proj, null, null, null);
				int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
				cursor.moveToFirst();
				path = cursor.getString(column_index);
			}catch(Exception e){
				
			}
			
			Bundle extras = data.getExtras();
			if(extras!=null){
				Bitmap photo = extras.getParcelable("data");
				ByteArrayOutputStream stream = new ByteArrayOutputStream();
				photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	//裁剪图片
	public void startPhotoZoom(Uri uri){
		Intent intent = new Intent("com.android.camera.action.CROP");	//调用系统自带的裁剪
		intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
		intent.putExtra("crop", "true");	//进行裁剪
		//设置宽高比例
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		//设置裁剪图片的宽高
		intent.putExtra("outputX", 320);
		intent.putExtra("outputY", 320);
		intent.putExtra("return-data", true);
		startActivityForResult(intent, PHOTO_RESOULT);
	}
	
	
}
